from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from appium.webdriver.common.touch_action import TouchAction
from time import sleep
from appium import webdriver
from common.mylog import MyLogger
from common.base_driver import init_driver
from common.getImage import SaveImage,save_screenshot
from selenium.webdriver.support import expected_conditions as EC
log = MyLogger("basePage").get_logger()
class basePage:

        driver=init_driver()

        def find_element(self, loc, time=10, poll=0.5):
            '''显示等待封装(element)'''
            #return WebDriverWait(self.driver, time, poll).until(lambda x: x.find_element(loc[0], loc[1]))
            try:
                log.info("开始等待页面元素<{}>".format(loc))
                ele = WebDriverWait(self.driver, time, poll).until(lambda x: x.find_element(loc[0], loc[1]))
                log.info("等待页面元素<{}> ==>成功".format(loc))
                return ele
            except :
                 print("页面元素<{}>等待存在失败！".format(loc[1]))
                 log.error("页面元素<{}>等待存在失败！".format(loc[1]))
                 SaveImage(self.driver,"页面元素<{}>等待失败的界面.png".format(loc[1]))
        def find_elements(self, loc, time=10, poll=0.5):
            '''显示等待封装(elements)'''
            try:
                log.info("开始等待页面元素<{}>".format(loc))
                ele = WebDriverWait(self.driver, time, poll).until(lambda x: x.find_elements(loc[0], loc[1]))
                log.info("等待页面元素<{}> ==>成功".format(loc))
                return ele
            except :
                print("页面元素<{}>等待存在失败！".format(loc[1]))
                log.error("页面元素<{}>等待存在失败！".format(loc[1]))
                SaveImage(self.driver, f"页面元素<%s>等待失败的界面.png" % (loc[1]))
        def click(self, loc):
            '''点击封装'''
            self.find_element(loc).click()

        def input(self, loc, text):
            '''输入封装'''
            self.find_element(loc).send_keys(text)

        def find_uiautomator(self, loc,instance=0,time=10, poll=0.5):
            '''显示等待uiautomato，instance：选择第n位'''

            try:
                loc1 = '"%s"' % loc
                log.info("开始等待页面元素<{}>".format(loc))
                UiSelector = 'new UiSelector().textContains(%s).instance(%s)' % (loc1,instance)
                ele=WebDriverWait(self.driver, time, poll).until(
                    lambda driver: driver.find_element_by_android_uiautomator(UiSelector))
                log.info("等待页面元素<{}> ==>成功".format(loc))
                return ele
            except:
                print("页面元素<{}>等待存在失败！".format(loc))
                SaveImage(self.driver,"页面元素{}等待存在失败.png".format(loc))
                log.error("页面元素<{}>等待存在失败！".format(loc))
        def touch_tap(self, x, y):
            '''模拟点击封装,输入x,y坐标'''
            return TouchAction(self.driver).press(x=x, y=y).release().perform()

        def slide_toUp(self,second=1,during=1000):
            '''上滑屏幕,second:滑动n次，during:滑动时间(ms)'''
            width = self.driver.get_window_size()['width']
            height = self.driver.get_window_size()['height']
            #second+=second
            for i in range(second):
                self.driver.swipe(width/2, height*3/4, width/2, height/4, during)
                #sleep(1)

        def swipe_up(self, t=500, n=1):
            s = self.driver.get_window_size()
            x1 = s['width'] * 0.5  # x坐标
            y1 = s['height'] * 0.75  # 起点y坐标
            y2 = s['height'] * 0.25  # 终点y坐标
            #print('手机的尺寸是： ', s)
            for i in range(n):
                self.driver.swipe(x1, y1, x1, y2, t)
        def slide_toDown(self,second=1,during=1000):
            '''下滑屏幕,second:滑动n次，during:滑动时间(ms)'''
            second+=second
            width = self.driver.get_window_size()['width']
            height = self.driver.get_window_size()['height']
            for i in range(1,second):
                    self.driver.swipe(width/2, height/4, width/2, height*3/4, during)
        def slide_toLeft(self,second=1,during=1000):
            '''左滑屏幕,second:滑动n次，during:滑动时间(ms)'''
            second+=second
            width = self.driver.get_window_size()['width']
            height = self.driver.get_window_size()['height']
            for i in range(1,second):
                    self.driver.swipe(width*3/4,height/2,width/4,height/2, during)
        def slide_toRight(self,second=1,during=1000):
            '''右滑屏幕,second:滑动n次，during:滑动时间(ms)'''

            second+=second
            width = self.driver.get_window_size()['width']
            height = self.driver.get_window_size()['height']
            for i in range(1,second):
                    self.driver.swipe(width/4,height/2,width*3/4, height/2, during)

        def get_toast_msg(self, partial_text, img_doc,time=10, poll=0.5):
            '''
            获取toast文本信息
            :param partial_text: 不完整文本
            :param img_doc: 截图说明
            :return: toast文本
            '''
            loc = (By.XPATH, '//*[contains(@text,"{}")]'.format(partial_text))
            try:
                WebDriverWait(self.driver, time, poll).until(EC.presence_of_element_located(loc))
                msg = self.driver.find_element(*loc).text
                print("toast出现了！！！")
                return msg
            except Exception as e:
                print("好可惜，toast没找到！！")
                log.error("获取toast文本失败！")
                save_screenshot(img_doc)
                raise e




if __name__ == '__main__':
    loc = "驾车"
    loc='"%s"'%loc
    #loc='"驾车"'
    UiSelector='new UiSelector().textContains(%s)'%loc
    print(UiSelector)